package com.wx.registerclient.config;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.nd4j.shade.jackson.core.JsonProcessingException;
import org.nd4j.shade.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import java.io.InputStream;

/**
 * 远程调用类
 */
@Component
public class RemoteTransfer {

        public <T> T  getResult(Class<T> tClass,String url, Object[] args){

            ObjectMapper mapper = new ObjectMapper();
            CloseableHttpClient build = HttpClientBuilder.create().build();

            HttpPost httpGet = new HttpPost(url);

            // 配置信息
            RequestConfig requestConfig = RequestConfig.custom()
                    // 设置连接超时时间(单位毫秒)
                    .setConnectTimeout(2000)
                    // 设置请求超时时间(单位毫秒)
                    .setConnectionRequestTimeout(2000)
                    // socket读写超时时间(单位毫秒)
                    .setSocketTimeout(2000)
                    // 设置是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();
            // 将上面的配置信息 运用到这个Get请求里
            httpGet.setConfig(requestConfig);
            // 请求的参数
            String json="";
            if(args!=null){
                for (Object arg : args) {
                    try {
                        json += mapper.writeValueAsString(arg);
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println(json);
            StringEntity entity = new StringEntity(json, "utf-8");//也可以直接使用JSONObject
            httpGet.setEntity(entity);
            httpGet.setHeader("Content-Type", "application/json;charset=utf8");

            // 发送请求
            try {
                CloseableHttpResponse execute = build.execute(httpGet);

                String name = tClass.getName();
                if("void".equals(name)){
                    return null;
                }

                // 获取响应的实体
                HttpEntity entity2 = execute.getEntity();
                // 获得输入流
                InputStream content = entity2.getContent();
                // 读取json字符串
                StringBuffer stringBuffer = new StringBuffer();
                int i=0;
                byte[] bytes = new byte[1024];
                while ((i=content.read(bytes))!=-1){
                    String json1 = new String(bytes, 0, i);
                    stringBuffer.append(json1);
                }

                T t = new ObjectMapper().readValue(stringBuffer.toString(), tClass);
                return t;
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }


}
